package org.spindle.soasuite.controllers;

import org.spindle.soasuite.core.api.AExecutable;
import org.spindle.soasuite.core.api.annotations.Executable;
import org.spindle.soasuite.core.api.annotations.ParameterList;
import org.spindle.soasuite.core.constants.StateConstants;
import org.spindle.soasuite.core.logging.Log;
import org.spindle.soasuite.core.logging.LogFactory;
import org.spindle.soasuite.data.internal.Configuration;
import org.spindle.soasuite.data.internal.MockService;
import org.spindle.soasuite.data.internal.Project;
import org.spindle.soasuite.data.internal.User;

import java.util.List;
import java.util.Map;

@Executable(path = "/endpoint")
public class MockController extends AExecutable {

    private static Log log = LogFactory.getLogger(MockController.class);

    @ParameterList
    private Map<String, String[]> parameters;

    @Override
    public Object execute() {
        String returnObject = null;

        MockService service = getInternalDatasource().getEndpointDAO().getEndpoint(getPath());
        if (service.getEndpoint().equals(getPath())) {
            for (Configuration config : service.getConfigurations()) {
                if (config.getCondition() == null || config.getCondition().equals("")) {
                    returnObject = config.getItemRenderer();
                    setReturnType(StateConstants.XML_TYPE);
                }
            }
        }

        if (returnObject != null) {
            String what;
            for (String key : parameters.keySet()) {
                what = "\\$\\{" + key + "\\}";
                returnObject = returnObject.replaceAll(what, parameters.get(key)[0]);
            }
        }

        return returnObject;

    }

    @Override
    public String getName() {
        return "Mock Controller";
    }
}
